/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CalendarView.java
 *
 * Created on 13/03/2011, 15:09:42
 */
package wf.ui.cmps;

import bc.dsl.SwingDSL;
import bc.utils.CalendarUtils;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Calendar;
import java.util.List;
import javax.swing.JLabel;
import org.jdesktop.swingx.JXPanel;
import wf.model.Schedule;
import wf.units.DatabaseUnit;

/**
 *
 * @author bennyl
 */
public class CalendarDaysView extends javax.swing.JPanel {

    private static final Color grayedOutDayColor = new java.awt.Color(204, 204, 204);
    private static final Color dayColor = new java.awt.Color(131, 192, 232);
    private static final Color currentDayCaptionColor = new Color(255, 0, 0);
    private DayContainer[] dayContainers;
    private Listener lis;
    private int day;
    private int month;
    private int year;
    private int selected = 1;
    private int firstDayCell = 0;
    private int daysInMonth = 0;
    private  int daysToShow = 35;

    /** Creates new form CalendarView */
    public CalendarDaysView() {
        Calendar now = Calendar.getInstance();

        this.day = now.get(Calendar.DAY_OF_MONTH);
        this.month = now.get(Calendar.MONTH) + 1;
        this.year = now.get(Calendar.YEAR);
        this.selected = this.day;

        initComponents();
        initializeDayContainers();
        setSelected(selected);
    }

    public void setListener(Listener lis) {
        this.lis = lis;
    }

    public CalendarDaysView(int day, int month, int year) {

        this.day = day;
        this.month = month;
        this.year = year;
        this.selected = this.day;

        initComponents();
        initializeDayContainers();
        setSelected(selected);
        markDaysWithScheduling(month, year);
    }

    private int firstDayInMonth() {
        Calendar c = Calendar.getInstance();
        c.set(year, month-1, 1);
        int firstDayInMonth = c.get(Calendar.DAY_OF_WEEK) - 1;
        return firstDayInMonth;
    }

    private void markDaysWithScheduling(int month, int year) {
        List<Schedule> l = DatabaseUnit.UNIT.selectSchedulesForMonth(month, year);
        for (Schedule li : l) {
            dayContainers[firstDayCell + li.getDayTime().getDate() - 1].containsLabel.setVisible(true);
        }
    }

    
    private void initializeDayContainers() {
        firstDayCell = firstDayInMonth();
        daysInMonth = CalendarUtils.daysInMonth(month, year);
        daysToShow = (int) Math.ceil((double) (firstDayCell + daysInMonth) / 7) * 7;
        dayContainers = new DayContainer[daysToShow];
        
        ((GridLayout)getLayout()).setRows(daysToShow / 7);
        
        int lastMonth = month - 1;
        int lastYear = year;
        if (lastMonth < 0) {
            lastMonth = 11;
            lastYear--;
        }
        
        int daysInLastMonth = CalendarUtils.daysInMonth(lastMonth, lastYear);
        
        for (int i=0; i<firstDayCell; i++){
            final int day = daysInLastMonth - firstDayCell + i + 1;
            dayContainers[i] = new DayContainer(day);
            dayContainers[i].panel.setBackground(grayedOutDayColor);
            add(dayContainers[i].panel);
            dayContainers[i].containsLabel.setVisible(false);
        }


        for (int i = firstDayCell; i < firstDayCell + daysInMonth; i++) {
            final int day = i -firstDayCell + 1;
            
            dayContainers[i] = new DayContainer(day);
            add(dayContainers[i].panel);
            dayContainers[i].containsLabel.setVisible(false);
            dayContainers[i].panel.setBackground(dayColor);
            dayContainers[i].panel.addMouseListener(new MouseAdapter() {

                @Override
                public void mouseClicked(MouseEvent e) {
                    super.mouseClicked(e);
                    setSelected(day);
                }
            });
        }
        
        for (int i=firstDayCell + daysInMonth; i<daysToShow; i++){
            final int day = i - firstDayCell - daysInMonth + 1;
            dayContainers[i] = new DayContainer(day);
            add(dayContainers[i].panel);
            dayContainers[i].containsLabel.setVisible(false);
            dayContainers[i].panel.setBackground(grayedOutDayColor);
        }

        colorCurrentDay();
//        colorMonthDays();
    }

    public void setSelected(int selected) {
        Calendar cdt = Calendar.getInstance();
        cdt.set(year, month - 1, day);

        if (selected <= cdt.getActualMaximum(Calendar.DAY_OF_MONTH)) {
            DayContainer dc = dayContainers[firstDayCell + this.selected - 1];
            dc.panel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(102, 102, 102)));
            dc.panel.setBackground(dayColor);
            this.selected = selected;
            dc = dayContainers[firstDayCell + selected - 1];
            dc.panel.setBackground(new java.awt.Color(255, 204, 102));
            dc.panel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 153, 0), 3));

            if (lis != null) {
                lis.selectionChanged(this.selected);
            }
        }
    }

    public int getSelected() {
        return selected;
    }

    private void colorCurrentDay() {
        Calendar now = Calendar.getInstance();
        if (month == now.get(Calendar.MONTH) + 1 && year == now.get(Calendar.YEAR)) {
            dayContainers[firstDayCell +  now.get(Calendar.DAY_OF_MONTH) - 1].datelabel.setForeground(currentDayCaptionColor);
        }
    }

//    private void colorMonthDays() {
//        Calendar cdt = Calendar.getInstance();
//        cdt.set(year, month - 1, day);
//        int i = 0;
//        for (; i < cdt.getActualMaximum(Calendar.DAY_OF_MONTH); i++) {
//            dayContainers[i].panel.setBackground(dayColor);
//        }
//
//        for (; i < DAYS_TO_SHOW; i++) {
//            dayContainers[i].panel.setBackground(grayedOutDayColor);
//        }
//    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        glossPainter1 = new org.jdesktop.swingx.painter.GlossPainter();

        glossPainter1.setInterpolation(org.jdesktop.swingx.painter.AbstractPainter.Interpolation.Bicubic);

        setBackground(new java.awt.Color(255, 255, 255));
        setMinimumSize(new java.awt.Dimension(800, 600));
        setPreferredSize(new java.awt.Dimension(800, 600));
        setLayout(new java.awt.GridLayout(5, 7, 3, 3));
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.jdesktop.swingx.painter.GlossPainter glossPainter1;
    // End of variables declaration//GEN-END:variables

    // End of variables declaration
    private class DayContainer {

        public JLabel datelabel;
        public JLabel containsLabel;
        public JXPanel panel;

        public DayContainer(int day) {

            this.datelabel = new JLabel();
            this.containsLabel = new JLabel();
            this.panel = new JXPanel();

            panel.setBackground(dayColor);
            panel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(102, 102, 102)));
            panel.setBackgroundPainter(glossPainter1);
            panel.setLayout(new java.awt.GridBagLayout());

            datelabel.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
            datelabel.setForeground(new java.awt.Color(153, 153, 153));
            if (day > 0) {
                datelabel.setText("" + day);
            }else{
                datelabel.setText("-");
            }
            
            GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 0;
            gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
            gridBagConstraints.weightx = 1.0;
            gridBagConstraints.weighty = 1.0;
            gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
            panel.add(datelabel, gridBagConstraints);

            containsLabel.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
            containsLabel.setForeground(new java.awt.Color(153, 153, 153));
            containsLabel.setIcon(SwingDSL.resIcon("calendar-event"));//(new javax.swing.ImageIcon(getClass().getResource("/waterfall/view/img/calendar-event.png"))); // NOI18N
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 0;
            gridBagConstraints.weightx = 1.0;
            gridBagConstraints.weighty = 1.0;
            gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
            panel.add(containsLabel, gridBagConstraints);
        }
    }

    public static interface Listener {

        void selectionChanged(int selection);
    }
}
